//
//  THJSlagsVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/7/7.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJSlagsVC.h"

@interface THJSlagsVC ()
{
    NSMutableArray * _bigpriceArr;
}
@property (weak, nonatomic) IBOutlet PYZoomEchartsView *chartView;
@property (nonatomic,strong)NSArray * dateArr;
@property (nonatomic,strong)NSMutableArray * nameArr;
@property (weak, nonatomic) IBOutlet UILabel *unitLab;

@end

@implementation THJSlagsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR(34, 34, 34, 1);
    self.tabBarController.tabBar.hidden = YES;
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:self.pageTitle];
    }else{
        [self setNavBarWithTitle:self.pageTitle];
    }
    [self loadDataWithTypeId:nil];
    if ([self.para1 isEqual:@"city"]) {
        self.unitLab.text = @"单位：百分之";
    }
    if ([self.type isEqual:@"waipan"]) {
        self.unitLab.text = @"单位：美元/吨度";
    }
  
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)loadDataWithTypeId:(NSString*)typeId{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    
    __weak typeof(self)weakself = self;
    [THJNetWork getNetDataFromUrl:self.url callback:^(NSDictionary* receiveData, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        NSLog(@"LL-%@",receiveData[@"data"]);
        weakself.unitLab.text = receiveData[@"data"][@"subtext"];
        weakself.dateArr = receiveData[@"data"][@"timedata"];
        NSArray * arr = receiveData[@"data"][@"list"];
        _bigpriceArr = [NSMutableArray array];
        for (NSDictionary * dic in arr ) {
            NSLog(@"bigdata--%@",dic);
            NSMutableArray * priceArr = [NSMutableArray array];
            for (NSString * price in dic[@"dataList"]) {
                if ([price floatValue] == 0) {
                    [priceArr addObject:[NSNull null]];
                }else{
                    [priceArr addObject:[NSNumber numberWithFloat:[price floatValue]]];
                }
            }
            [_bigpriceArr addObject:priceArr];
        }
        
        _nameArr =  receiveData[@"data"][@"typeList"];
       
        [weakself setChartViewData];
    }];
}
-(void)setChartViewData {
//    NSMutableArray * dateArray = [NSMutableArray array];
//    for (NSString * str in self.dateArr) {
//        NSArray *  arr =  [str componentsSeparatedByString:@"-"];
//        NSString * str = [NSString stringWithFormat:@"%@-%@",arr[1],arr[2]];
//        [dateArray addObject:str];
//    }
//    
    [_chartView setOption:[self standardLineOptionWithArr1:_bigpriceArr DateArr:self.dateArr]];
    [_chartView loadEcharts];
}
- (PYOption *)standardLineOptionWithArr1:(NSArray*)arr1 DateArr:(NSArray*)dateArr{
    NSNumber * value;
    if ([self.para1 isEqual:@"name"]) {
        value = @(80);
    }else{
        value = @(40);
    }

    return [PYOption initPYOptionWithBlock:^(PYOption *option) {
        option
        .tooltipEqual([PYTooltip initPYTooltipWithBlock:^(PYTooltip *tooltip) {
            tooltip.triggerEqual(PYTooltipTriggerAxis);
        }])
        .legendEqual([PYLegend initPYLegendWithBlock:^(PYLegend *legend) {
            legend.dataEqual(_nameArr);
            legend.textStyle.colorEqual(@"#ffffff");
        }])
        .gridEqual([PYGrid initPYGridWithBlock:^(PYGrid *grid) {
            grid.xEqual(@45).x2Equal(@20);
            grid.yEqual(value).y2Equal(@30);
        }])
        .addXAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
            axis.typeEqual(PYAxisTypeCategory)
            .boundaryGapEqual(@NO)
            .addDataArr(dateArr);
            axis.axisLabel.textStyleEqual([PYTextStyle initPYTextStyleWithBlock:^(PYTextStyle *textStyle) {
                textStyle.colorEqual(@"#ffffff");
            }]);
        }])
        .addYAxis([PYAxis initPYAxisWithBlock:^(PYAxis *axis) {
            axis.typeEqual(PYAxisTypeValue);
            axis.axisLabel.textStyleEqual([PYTextStyle initPYTextStyleWithBlock:^(PYTextStyle *textStyle) {
                textStyle.colorEqual(@"#ffffff");
            }]);
        }]);
        for (NSInteger i = 0;i< arr1.count;i++) {
            option.addSeries([PYSeries initPYSeriesWithBlock:^(PYSeries *series) {
                series.nameEqual(_nameArr[i])
                .typeEqual(PYSeriesTypeLine)
                .dataEqual(arr1[i]);
            }]);
        }
    }];
}

@end
